const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serverImgs: [{
        img: '../../images/my/haibaoku.png',
        text: '海报库'
      },
      {
        img: '../../images/my/xiaoketang.png',
        text: '小课堂'
      },
      {
        img: '../../images/my/choujiang.png',
        text: '我的抽奖'
      },
      {
        img: '../../images/my/yiuhuiquan.png',
        text: '优惠券'
      }, {
        img: '../../images/my/shoucang.png',
        text: '我的收藏'
      },
      {
        img: '../../images/my/liulanqi.png',
        text: '最近浏览'
      },
      {
        img: '../../images/my/address.png',
        text: '收货地址'
      },
      {
        img: '../../images/my/shouhou.png',
        text: '退换售后'
      },
      {
        img: '../../images/my/kefu-help.png',
        text: '客服帮助'
      }
    ],
    isLogin: false,
    userInfo: null,
    avatarUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let userBean = app.globalData.userBean;
    if (userBean) {
      this.setData({
        userInfo: userBean,
        isLogin: true
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(app.globalData.userBean)
    var that = this;
    let userBean = app.globalData.userBean;
    var isLogin = false;
    var avatarUrl = app.globalData.defaultAvatarUrl;
    // if (userBean && userBean.userId) {
    if (userBean) {
      isLogin = true;
      avatarUrl = userBean.avatar || app.globalData.defaultAvatarUrl;
    } else {
      isLogin = false;
    }
    that.setData({
      isLogin: isLogin,
      userInfo: userBean,
      avatarUrl: avatarUrl
    })
    console.log(this.data.isLogin)
    //如果未登录,则进行登录
    if (!isLogin) {
        wx.stopPullDownRefresh();
        wx.navigateTo({
            url: '../login/login-type/index' // 指定页面的url
        });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function(options) {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  toLogin: function() {
    wx.navigateTo({
      url: '../login/login-type/index'
    })

  },
  toServerPages: function(e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    switch (index) {
      case 0:
        wx.navigateTo({
          url: '../servers/posters-library/posters-library' // 指定页面的url
        });
        break;
      case 1:
        wx.navigateTo({
          url: '../servers/small-class/index' // 指定页面的url
        });
        break;
      case 2:
        wx.navigateTo({
          url: '../servers/my-lucky-index/my-lucky-index' // 指定页面的url
        });
        break;
      case 3:
        wx.navigateTo({
          url: '../servers/coupons-index/index' // 指定页面的url
        });
        break;
      case 4:
        wx.navigateTo({
          url: '../servers/my-collect-index/my-collect-index?type=0' // 指定页面的url
        });
        break;
      case 5:
        wx.navigateTo({
          url: '../servers/my-collect-index/my-collect-index?type=1' // 指定页面的url
        });
        break;
      case 6:
        wx.navigateTo({
          url: '../servers/addres-index/addres-index' // 指定页面的url
        });
        break;
      case 7:
        wx.navigateTo({
          url: '../servers/after-sale-index/after-sale-index' // 指定页面的url
        });
        break;
      case 8:
        wx.navigateTo({
          url: '../servers/kefu-help/index' // 指定页面的url
        });
        break
    }
  },
  toMyOrder: function() {
    wx.navigateTo({
      url: '../my-order/order-list/index?curTap=0' // 指定页面的url
    });
  },
  orderNoPay: function() {
    wx.navigateTo({
      url: '../my-order/order-list/index?curTap=1' // 指定页面的url
    });
  },
  orderNoPing: function() {
    wx.navigateTo({
      url: '../my-order/order-list/index?curTap=2' // 指定页面的url
    });
  },
  orderNoSend: function() {
    wx.navigateTo({
      url: '../my-order/order-list/index?curTap=3' // 指定页面的url
    });
  },
  orderNoReceive: function() {
    wx.navigateTo({
      url: '../my-order/order-list/index?curTap=4' // 指定页面的url
    });
  },
  orderNoEvaluate: function() {
    wx.navigateTo({
      url: '../my-order/order-list/index?curTap=5' // 指定页面的url
    });
  },
  toBalance: function() {
    wx.navigateTo({
      url: '../my-money/wallet-balance/index' // 指定页面的url
    });
  },
  toGold: function() {
    wx.navigateTo({
      url: '../my-money/my-gold/index' // 指定页面的url
    });
  },

  toInvited: function() {
    wx.navigateTo({
      url: '../my-money/invite-index/index'
    })
  },
  toEarnings: function() {
    wx.navigateTo({
      url: '../my-money/my-earnings/index'
    })
  },
  toUserIndex: function() {
    wx.navigateTo({
      url: '../user-info/user-index/index', // 指定页面的url
    });
  }
})